﻿<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>模块信息</title>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <link href="../lib/layui/css/layui.min.css" rel="stylesheet" />
    <link href="../x-admin/css/font.css" rel="stylesheet" />
    <link href="../x-admin/css/xadmin.css" rel="stylesheet" />
    <link href="../lib/layui/lay/plugins/eleTree/font/eleTree.css" rel="stylesheet" />
    <script type="text/javascript" src="../lib/Base64/base64.min.js"></script>
    <script type="text/javascript" src="../linq.js/linq.js"></script>
    <script src="../lib/cookie/cookie.js"></script>
    <style>
        .unoperate {
            display: none;
        }

    </style>
</head>
<body style="background-color:#eeeeee">
    <div class="x-nav" style="background-color:#ffffff">
        <span class="layui-breadcrumb">
            <a href="">模块配置</a>
            <a>
                <cite>配置文件列表</cite>
            </a>
        </span>
        <a class="layui-btn layui-btn-small" style="float:right" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-1"></i>
        </a>
    </div>
    <div class="x-body">
        <div class="layui-col-md2"style="clear:both;">
            <div class="layui-card"style="height:600px; ">
                <div class="layui-card-body">
                    <div class="eleTree" id="commonconfigtree" lay-filter="commonconfigtree" style="cursor:pointer"></div>
                </div>
            </div>
            
        </div>
        <div class="layui-col-md10">
            <div class="layui-card" style="min-height:600px;">
                <div class="layui-card-header" style="">
                    <button class="layui-btn operateBtn unoperate" type="button" id="addCatalogue">
                        <i class="layui-icon">&#xe61f;</i>添加目录
                    </button>
                    <button class="layui-btn operateBtn unoperate" type="button" id="uploadfile">
                        <i class="layui-icon">&#xe62f;</i>上传文件
                    </button>
                    <label class="layui-form-label" id="currentDirPathlbl" style="float:none;display:inline-block;"></label>
                </div>
                <div class="layui-card-body" style="">
                    <input type="hidden" id="currentDirPath" />
                    <form class="layui-form layui-form-pane" style="padding-top:10px;" lay-filter="commonconfigSearchForm">
                        <div class="layui-form-item" style="margin-bottom:0;">
                            <div class="layui-inline">
                                <label class="layui-form-label">文件名</label>
                                <div class="layui-input-block">
                                    <input class="layui-input" id="fileName" name="fileName" type="text" autocomplete="off" />
                                </div>
                            </div>
                            <div class="layui-inline">
                                <a class="layui-btn" href="javascript:;" lay-submit lay-filter="load" id="load">搜索</a>
                            </div>
                        </div>
                    </form>
                    <table class="layui-table layui-form" id="commonconfigTab" name="commonconfigTab" lay-filter="commonconfigTab">
                    </table>
                </div>
            </div>
        </div>
    </div>
    <script src="../lib/layui/layui.js"></script>
    <script type="text/html" id="barmoudle">
        <a href="javascript:;" lay-event="detail"><i class="layui-icon">&#xe63c;</i>查看</a>
        <a title="下载" href="javascript:;" class="operateBtn" lay-event="downloadfile"><i class="layui-icon">&#xe601;</i>下载</a>
        <a title="删除" href="javascript:;" class="operateBtn" lay-event="deletefile"><i class="layui-icon">&#xe640;</i>删除</a>
    </script>
    <script type="text/javascript">
        function getrequest() {
            var url = location.search; //获取url中"?"符后的字串 
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        layui.config({
            base: '/lib/layui/lay/plugins/'
        }).extend({
            eleTree: 'eleTree/eleTree'
        }).use(['eleTree', 'table','form'], function () {
            var eleTree = layui.eleTree;
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;
            $("#currentDirPath").val(getrequest().dir);
            $("#currentDirPathlbl").text(getrequest().dir);
            var el = eleTree({
                el: '#commonconfigtree',
                method: 'get',
                url: localStorage.getItem('hosttingaddr') + '/api/Commonconfig/GetCommonConfigCatalogue?dir=' + getrequest().dir,
                headers: {
                    user: getCookie("userinfo"),
                    version: getCookie("version")
                },
                defaultExpandAll: false,
                showLine: true,
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    dataName: 'data'
                },
                request: {
                    name: 'title',
                    key: 'href',
                    children: 'childrens',
                    checked: 'checked',
                    disabled: 'disabled',
                    isLeaf: 'isLeaf'
                }
                
            });
            $tab= table.render({
                elem: '#commonconfigTab'
                , method: 'get'
                , skin: 'row'
                , even: true
                , page: true
                , url: localStorage.getItem('hosttingaddr') + '/api/Commonconfig/getcommonconfigfile' //数据接口
                , headers: {
                    user: getCookie("userinfo"),
                    version: getCookie("version")
                }
                , cols: [[ //表头
                    { field: 'name', title: '文件名' }
                    , { field: 'note', title: '备注', edit: 'text' }
                    , { field: 'parentPath', title: '文件路径'}
                    , { field: 'fileSize', title: '文件大小' }
                    , { field: 'createdTime', title: '文件创建时间' }
                    , { field: 'updateTime', title: '文件更新时间' }
                    , { fixed: 'right', title: "操作",align: 'center', toolbar: '#barmoudle' }
                ]],
                where: getWhere(),
                done: function (res) {
                    if (res.isOperate != 1) {
                        layui.$(".operateBtn").addClass("unoperate");
                    }
                    else {
                        layui.$(".operateBtn").removeClass("unoperate");
                    }
                }
            });
            //监听单元格编辑
            table.on('edit(commonconfigTab)', function (obj) {
                var value = obj.value //得到修改后的值
                    , data = obj.data //得到所在行所有键值
                    , field = obj.field; //得到字段
                layer.msg('[ID: ' + data.name + '] ' + field + ' 字段更改值为：' + value + '路径为：' + data.parentPath);
            });
            el.on('click', function (re) {
                if (re != null) {
                    $("#currentDirPath").val(re.data.href);
                    $("#currentDirPathlbl").text(re.data.href);
                    form.val('commonconfigSearchForm', {
                        "fileName":""
                    })
                    table.reload("commonconfigTab", {
                        where: getWhere()
                    })
                }
            })

            layui.form.on("submit(load)", function () {
                var where = getWhere();
                $tab.reload({
                    where: where,
                    page: { curr: 1 }
                });
                return false;
            });

            //监听行工具事件
            table.on('tool(commonconfigTab)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                var fileN = data.path;
                if (layEvent == 'detail') {
                    var arr = fileN.split('/');
                    layer.open({
                        type: 2,
                        title: '公共配置文件内容',
                        maxmin: true,
                        shadeClose: true, //点击遮罩关闭层
                        area: ['1600px', '800px'],
                        content: 'commonconfigdetail.html' + '?fileName=' + fileN// iframe链接地址
                    });
                } else if (layEvent == 'downloadfile') {
                    layer.msg('下载文件');
                    //进行文件下载
                    var fileName = data.name;
                    var xhr = new XMLHttpRequest();
                    xhr.open("get", localStorage.getItem('hosttingaddr') + '/api/Commonconfig/downloadfile?fileName=' + fileN, true);
                    xhr.responseType = "blob";
                    xhr.setRequestHeader("user", getCookie("userinfo"));
                    xhr.setRequestHeader("version", getCookie("version"))
                    xhr.onload = function (data) {
                        if (this.status == 200) {
                            var blob = this.response;
                            var reader = new FileReader();
                            reader.readAsDataURL(blob);
                            reader.onload = function (e) {
                                var a = document.createElement('a');
                                a.download = fileName;
                                a.href = e.target.result;
                                document.body.appendChild(a);
                                a.click();
                                document.body.removeChild(a);
                            }
                        }
                    };
                    xhr.send();
                } else if (layEvent == 'deletefile') {
                    fileP = btoa(fileN);
                    layer.confirm('确认要删除吗？', function (index) {
                        //发异步删除数据
                        layui.$.ajax({
                            type: "post",
                            url: localStorage.getItem('hosttingaddr') + '/api/Commonconfig/delfile?fileName=' + fileN,
                            headers: {
                                user: getCookie("userinfo"),
                                version: getCookie("version")
                            },
                            success: function (data) {
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.msg('已删除!', { icon: 1, time: 1000 });
                                layer.close(index);
                            }
                        });

                    });
                }

            });
            function getWhere() {
                var where = layui.form.val("commonconfigSearchForm");
                where.dir = $("#currentDirPath").val();
                return where;
            }

        });
        //上传内容
        layui.use(['upload','table'], function () {
            var upload = layui.upload;
            var table = layui.table;
            var $ = layui.$;
            //执行实例
            var uploadInst = upload.render({
                elem: '#uploadfile', //绑定元素
                url: localStorage.getItem('hosttingaddr') + '/api/commonconfig/uploadfile',//上传接口
                headers: {
                    user: getCookie("userinfo"),
                    version: getCookie("version")
                },
                data: {
                    dir: function () {
                        return layui.$("#currentDirPath").val();
                    }
                },
                acceptMime: "text/xml",
                accept:"file",
                exts: "xml",
                multiple: true,//允许多文件上传
                
                done: function (res) {
                    //上传完毕回调
                    layer.msg(res.message);
                    //上传成功后进行数据刷新
                    table.reload("commonconfigTab", {
                        where: {
                            dir: layui.$("#currentDirPath").val()
                        }
                    })
                },
                error: function () {
                    //请求异常回调
                    layer.msg("文件上传失败！");
                }
            });
        });
        //添加目录按钮点击事件
        layui.jquery('#addCatalogue').click(function () {
            var dirPath = layui.$("#currentDirPath").val();
            addCatalogue(dirPath);
        });
        //添加目录
        function addCatalogue(dir) {
            var iteminfo = btoa(window.encodeURIComponent(JSON.stringify(dir)));
            layer.open({
                type: 2,
                title: '模块配置文件内容',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area: ['600px', '350px'],
                content: 'addcatalogue.html' + '?parentPath=' + iteminfo
            });
        }
    </script>
</body>
</html>